import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Screen extends JPanel implements Runnable {
	
	public Thread thread = new Thread(this);	// Creates thread 
	
	public static Image[] tileset_ground = new Image[100];
	public static Image[] tileset_air = new Image[100];
	public static Image[] tileset_res = new Image[100];
	public static Image[] tileset_mob = new Image[100];
	
	public static int coinage = 10, health = 100;
	
	public static boolean isFirst = true; 
	public static Point mse = new Point(0,0);
	public static int myWidth, myHeight; 
	public static Room room; 
	public static Save save;
	public static Store store;
	
	public static Mob[] mobs = new Mob[100];
	
	public Screen(Frame frame) {
		frame.addMouseListener(new KeyHandel());
		frame.addMouseMotionListener(new KeyHandel());
		thread.start();
	}

	public void define() {
		room = new Room(); 
		save = new Save();
		store = new Store();
		
		// Loads images
		for( int i = 0; i<tileset_ground.length;i++){
			tileset_ground[i] = new ImageIcon("res/tileset_ground.png").getImage();
			tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(),
					new CropImageFilter(0,(Room.blockSize) * i,(Room.blockSize),(Room.blockSize))));
		}
		
		for( int i = 0; i<tileset_air.length;i++){
			tileset_air[i] = new ImageIcon("res/tileset_ground.png").getImage();
			tileset_air[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(),
					new CropImageFilter(0,Room.blockSize * i,Room.blockSize,Room.blockSize)));
		}
		
		tileset_res[0] = new ImageIcon("res/cell.png").getImage();
		tileset_mob[0] = new ImageIcon("res/cell.png").getImage();
		
		tileset_air[2] = tileset_res[0]; 	// setting air icon to icon 
		
		// Reads save
		save.loadSave(new File("save/mission1.tom"));
		
		for(int i=0;i<mobs.length;i++){
			mobs[i] = new Mob();
			// mobs[i].spawnMob(0);
		}
		
	}
	
	/* 
	 * When you subclass JComponent or JPanel to draw graphics, override the paintComponent() method. 
	 * This method is called because the user did something with the user interface that required 
	 * redrawing, or your code has explicitly requested that it be redrawn.(non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g) {
		
		if(isFirst) {
			myWidth = getWidth();
			myHeight = getHeight();
			define();
			isFirst = false;
		}
		g.setColor(new Color(50,50,50));
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(new Color(0,0,0));
		// left border
		g.drawLine(room.block[0][0].x -1, 
				0, 
				room.block[0][0].x -1, 
				room.block[room.worldHeight-1][0].x + room.blockSize + 1);
		// left border
		g.drawLine(room.block[0][0].x + room.blockSize, 
					0, 
					room.block[0][0].x -1, 
					room.block[room.worldHeight-1][0].x + room.blockSize + 1);
		room.draw(g); // Drawing the room
		
		for(int i=0;i<mobs.length;i++){
			if(mobs[i].inGame){
				mobs[i].draw(g);
			}
		}
		store.draw(g);
	}

	public static int fpsFrame = 0, fps = 1000000;
	
	public int spawnTime = 1000, spawnFrame = 0;
	public void mobSpawner() {
		if(spawnFrame >= spawnTime){
			for(int i=0;i<mobs.length;i++){
				if(!mobs[i].inGame){
					mobs[i].spawnMob(Value.mobGreeny);
					break;
				}
			}
			spawnFrame = 0;
		} else {
			spawnFrame += 1;
		}
	}
	
	@Override
	public void run() {		// Called by thread
		while(true) {
			if(!isFirst) {
				room.physic();
				mobSpawner();
				for(int i=0;i<mobs.length;i++){
					if(mobs[i].inGame){
						mobs[i].physic();
						
					}
				}
			}
			
			repaint();
			
			try {
				Thread.sleep(1);
			} catch (Exception e) {} 
		}
	}
}
